package cn.itsy.web.controller;

import cn.itsy.pojo.Emp;
import cn.itsy.pojo.Job;
import cn.itsy.service.job.JobService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: MisterGay
 * @date: 2019-05-31 9:25
 */
@Controller
@RequestMapping("job")
public class JobController {
    @Resource(name = "jobServiceimpl")
    private JobService jobService;

    /**
     * 添加职位
     *
     * @param job
     */
    @RequestMapping(value = "insertJob", method = RequestMethod.GET)
    @ResponseBody
    public String insertJob(Job job) {
        String sql = "insert into job values(?,?,?,?,?,?,?)";
        this.jobService.insertJob(sql, job);
        return "success";
    }

    /**
     * 查询职位
     */
    @RequestMapping(value = "queryJobAll", method = RequestMethod.GET)
    @ResponseBody
    public List<Job> queryJobAll() {
        String sql = "select job.*,dept.dname,duct.zname from job,duct,dept where job.duct_id=duct.id" +
                " and job.dept_id = dept.id";
        List<Job> jobs = this.jobService.queryJobAll(sql);
        return jobs;
    }

    /**
     * 修改职位不包括id
     */
    @RequestMapping(value = "updateJobNotId", method = RequestMethod.POST)
    @ResponseBody
    public void updateJobNotId(Job job, Integer checkId) {
        if (job.getId() == null || job.getId() == 0) {
            job.setId(checkId);
        }
        String sql = "update job set id=?,number=?,name=?,duct_id=?,dept_id=?,wren=?,winfo=? where id=?";
        this.jobService.updateJobById(sql, job);
    }

    @RequestMapping(value = "updateJobById", method = RequestMethod.POST)
    @ResponseBody
    public void updateJobById(Job job, Integer checkId) {
        String sql = "update job set id=? where id=?";
        this.jobService.updateJobById(sql, job, checkId);
        String empSql = "update emp set job_id=? where job_id=?";
        this.jobService.updateEmpByJobId(empSql, job, checkId);
    }

    @RequestMapping(value = "deleteDucTypeById")
    @ResponseBody
    public Integer deleteDucTypeById(Job job) {
        String sql = "select * from emp where emp.job_id=?";
        List<Emp> emps = this.jobService.queryEmpById(sql, job);
        if (emps.isEmpty()) {
            String delSql = "delete from job where job.id=?";
            this.jobService.deleteJobById(delSql, job);
            return 1;
        }
        return 0;
    }
}
